package com.chainyoung.component_tmperson.ui.fragment;


import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.adpter.BaseFragmentAdapter;
import com.chainyoung.common.base.BaseFragment;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.event.BaseEvent;
import com.chainyoung.common.utils.CommonUtil;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.ui.activity.cloudwallet.SelectCoinTypeActivity;
import com.chainyoung.component_tmperson.ui.activity.UserCenterActivity;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import q.rorbin.badgeview.QBadgeView;

import static com.chainyoung.common.event.BaseEvent.EventCode.IS_ASSET_VISIBLE;
import static com.chainyoung.component_tmperson.ui.fragment.MyPurseAddrListFragment.TYPE_CLOUD;
import static com.chainyoung.component_tmperson.ui.fragment.MyPurseAddrListFragment.TYPE_LOCAl;

/**
 * A simple {@link Fragment} subclass.
 */


public class WalletFragment extends BaseFragment {

    @BindView(R2.id.total_amount)
    TextView totalAmount;
    //    @BindView(R2.id.fl)
//    FrameLayout fl;
    @BindView(R2.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;


    //private LoadService loadService;
    private QBadgeView badgeView;


    //  private WalletAdapter walletAdapter;
    //  private List<Purse> mData = new ArrayList<>();

    /**
     * 总资产
     */
    private String totalAsset = "";

    /**
     * 资产是否可见
     */
    private boolean isVisible = true;

    private String[] titles = {"云端钱包", "本地钱包"};
    private int[] drawableRs = {R.drawable.selector_wallet_cloud, R.drawable.selector_wallet_local};
    private List<Fragment> fragmentList = new ArrayList<>();


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
//        DaggerPersonFragmentComponent.builder()
//                .appComponent(appComponent)
//                .personFragmentModule(new PersonFragmentModule(this))
//                .build()
//                .inject(this);
    }

    @Override
    protected void initUi() {
        super.initUi();

       // showAndSetBarRightIcon(R.mipmap.ic_msg);
        showAndSetBarLeftIcon(R.mipmap.ic_visible);
        setCurrentTitle(R.string.asset);
        isVisible = SPUtil.getBoolean(LibConstant.isShowAsset, true);
        mBarLeftImageView.setImageResource(isVisible ? R.mipmap.ic_visible : R.mipmap.ic_invisible);
        totalAmount.setText(isVisible ? "CNY " + totalAsset : "CNY ****");
        initTabLayout();
        initMsgNum();
        //initLoadSir();
    }

    private void setVisibleView() {
        mBarLeftImageView.setImageResource(isVisible ? R.mipmap.ic_visible : R.mipmap.ic_invisible);
        totalAmount.setText(isVisible ? "CNY " + totalAsset : "CNY ****");
        //  walletAdapter.setShowAsset(isVisible);
        // walletAdapter.notifyDataSetChanged();
    }

    private void initTabLayout() {
        fragmentList.add(WalletCloudListFragment.newInstance(isVisible));
        fragmentList.add(WalletLocalListFragment.newInstance(isVisible));
        //fragmentList.add(WalletFinancingManageFramgent.newInstance());
        //fragmentList.add(WalletDrFragment.newInstance());
        //tabLayout.setTabMode(TabLayout.MODE_FIXED);
        viewPager.setAdapter(new BaseFragmentAdapter(getChildFragmentManager(), fragmentList));
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(titles.length);
        viewPager.setCurrentItem(0);
        setTabs();

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void setTabs() {
        for (int mI = 0; mI < titles.length; mI++) {
            TabLayout.Tab mTab = tabLayout.getTabAt(mI);
            View mView = LayoutInflater.from(mContext).inflate(R.layout.layout_wallet_tab, null);
            TextView mTabText = mView.findViewById(R.id.tv_tab_name);
            mTabText.setText(titles[mI]);
            // mTabText.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableRs[mI], 0, 0, 0);
            if (mTab != null) {
                mTab.setCustomView(mView);
            }
        }
    }

    private void initMsgNum() {
        int num = 0;
        if (CommonUtil.getUserInfo() != null) {
            num = CommonUtil.getUserInfo().getUnread_msg_num();
        }
       /* if (badgeView == null) {
            badgeView = new QBadgeView(mContext);
            badgeView.setBadgeBackgroundColor(ContextCompat.getColor(mContext, R.color._F95A54));
            badgeView.setBadgeTextColor(ContextCompat.getColor(mContext, R.color.white));
            badgeView.setBadgeTextSize(9, true);
            badgeView.bindTarget(mBarRightImageView).setBadgeGravity(Gravity.TOP | Gravity.END).
                    setGravityOffset(4, 3, true).
                    setBadgeNumber(num);
        } else {
            badgeView.setBadgeNumber(num);
        }*/
    }


    @Override
    public void onRightImgClicked(View v) {
        super.onRightImgClicked(v);
        /*RouterUtils.getInstance()
                .build(RouterConstants.PATH_MESSAGE_CENTER)
                .navigation();*/

    }


    @Override
    public void onLeftViewClicked(View view) {
        super.onLeftViewClicked(view);
        isVisible = !isVisible;
        setVisibleView();
        SPUtil.put(LibConstant.isShowAsset, isVisible);
        EventBus.getDefault().post(new BaseEvent<>(IS_ASSET_VISIBLE, isVisible));
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    public void onUserVisible() {
        super.onUserVisible();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_fragment_wallet;
    }


    //----------------------------------------------event-------------------------------------

    @Subscriber
    void clearMsgNum(BaseEvent event) {
        /*if (event.getCode() == BaseEvent.EventCode.CLEAR_MSG) {
            badgeView.setBadgeNumber(0);
        }*/
    }

    @Subscriber
    void updateMsgNum(BaseEvent event) {
        /*if (event.getCode() == BaseEvent.EventCode.MSG_NUM) {
            HashMap<String, Integer> map = (HashMap<String, Integer>) event.getData();
            int num = 0;
            for (Integer integer : map.values()) {
                num += integer;
            }
            badgeView.setBadgeNumber(num);
        }*/
    }

    @Subscriber
    void onGetNewWallet(BaseEvent event) {
        if (event.getCode() == BaseEvent.EventCode.IS_GET_NEW_WALLET) {
            viewPager.setCurrentItem(1, false);
            setVisibleView();
        }
    }



    @Subscriber
    void setAssetVisible(BaseEvent event) {
        if (event.getCode() == BaseEvent.EventCode.REFRESH_TOTAL_ASSET) {
            String localAsset = ((WalletLocalListFragment) fragmentList.get(1)).getTotalAsset();
            String cloudAsset = ((WalletCloudListFragment) fragmentList.get(0)).getTotalAsset();
            totalAsset = new BigDecimal(localAsset).add(new BigDecimal(cloudAsset)).setScale(2, RoundingMode.DOWN) + "";
            setVisibleView();
        }
    }


}
